﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using DBUtility.PetaPoco;
using Model;

namespace DAL
{
	/// <summary>
    /// 待推送消息 數據訪問類
    /// </summary>
    public class PendingpushmessageDAL : BaseDAL<Model.Pendingpushmessage>
    {
        /// <summary>
        /// 添加推送消息以及需要推送的設備
        /// </summary>
        /// <param name="AppId">項目名</param>
        /// <param name="Message">消息</param>
        /// <param name="DevicetokenList">設備集合</param>
        /// <param name="Type">類型</param>
        /// <returns></returns>
        public int AddList(int appId,string message,string data, string aztokenList,string iostokenList)
        {
            try
            {
                using (Transaction tran = db.GetTransaction())//开始事务
                {
                    Pendingpushmessage model = new Pendingpushmessage();
                    model.AppId = appId;
                    model.Messagecontent = message;
                    model.Data = data;
                    model.Createtime = DateTime.Now;
                    model.SendCount = 0;
                    #region 添加Pendingpushmessage
                    object objId = db.Insert(model);
                    if (objId == null || objId == DBNull.Value)
                    {
                        return 0;
                    }
                    #endregion

                    int id;
                    if (!int.TryParse(objId.ToString(), out id) || id == 0)
                    {
                        return 0;
                    }

                    object objIsSuccess = null;

                    if (aztokenList != "")
                    {
                        string[] aztoken = aztokenList.Split(',');
                        #region 添加Able_Channel_Info
                        if (aztoken != null && aztoken.Length > 0)
                        {
                            foreach (var token in aztoken)
                            {
                                Device de = new Device();
                                de.Mid = id;
                                de.Devicetoken = token;
                                de.Status = false;
                                de.Type = 2;
                                de.Count = 0;
                                objIsSuccess = db.Insert(de);

                                if (objIsSuccess == null || objIsSuccess == DBNull.Value)
                                {
                                    return 0;
                                }

                            }
                        }
                        #endregion
                    }

                    if (iostokenList != "")
                    {
                        string[] iostoken = iostokenList.Split(',');
                        #region 添加Able_Channel_Info
                        if (iostoken != null && iostoken.Length > 0)
                        {
                            foreach (var token in iostoken)
                            {
                                Device de = new Device();
                                de.Mid = id;
                                de.Devicetoken = token;
                                de.Status = false;
                                de.Type = 1;
                                de.Count = 0;
                                objIsSuccess = db.Insert(de);

                                if (objIsSuccess == null || objIsSuccess == DBNull.Value)
                                {
                                    return 0;
                                }

                            }
                        }
                        #endregion
                    }

                    tran.Complete();//提交事务
                    return id;
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 獲取需要推送的消息以及設備
        /// </summary>
        /// <returns></returns>
        public List<Pendingpushmessage> GetList()
        {
            try
            {
                //return db.Fetch<Pendingpushmessage>("select p.*,AppName,Certificate,PassWord,SenderId,AuthorizationToken,PackageName from Pendingpushmessage as p left join App as a on p.AppId=a.Id");
                return db.Fetch<Pendingpushmessage>("select p.*,AppName,Certificate,PassWord,SenderId,AuthorizationToken,PackageName from Pendingpushmessage as p left join App as a on p.AppId=a.Id where (select count(*) from device where Mid=p.Id and Status=0)>0");
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 已推送集合
        /// </summary>
        /// <returns></returns>
        public List<Pendingpushmessage> GetListSent()
        {
            try
            {
                return db.Fetch<Pendingpushmessage>("select p.Messagecontent,p.AppId,GROUP_CONCAT(d.Devicetoken separator ',') AS Devicelist,d.Type from Pendingpushmessage as p left join Device as d on p.Id=d.MId where d.Status=1 group by p.Messagecontent, p.AppId, d.Type");
            }
            catch
            {
                return null;
            }

        }
    }
}
